package com.qf.springbootproject.shopcart.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qf.springbootproject.index.entity.Product;
import com.qf.springbootproject.index.entity.ProductImg;
import com.qf.springbootproject.index.service.ProductImgService;
import com.qf.springbootproject.index.service.ProductService;
import com.qf.springbootproject.product.entity.ProductComments;
import com.qf.springbootproject.product.entity.ProductSku;
import com.qf.springbootproject.product.service.ProductSkuService;
import com.qf.springbootproject.shopcart.entity.ShoppingCart;
import com.qf.springbootproject.shopcart.mapper.ShoppingCartMapper;
import com.qf.springbootproject.shopcart.service.ShoppingCartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * <p>
 * 购物车  服务实现类
 * </p>
 *
 * @author yyq
 * @since 2023-02-16
 */
@Service
public class ShoppingCartServiceImpl extends ServiceImpl<ShoppingCartMapper, ShoppingCart> implements ShoppingCartService {
    @Autowired
    ProductService productService;
    @Autowired
    ProductImgService productImgService;
    @Autowired
    ProductSkuService productSkuService;

    /**
     * 购物车接口
     *
     * @param userId
     * @return
     */
    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public List<ShoppingCart> listShopCart(Integer userId) {
        List<ShoppingCart> cartList = list(new QueryWrapper<ShoppingCart>().eq("user_id", userId));
        populate(cartList);
        return cartList;
    }

    /**
     * 更新购物车商品数量
     *
     * @param shoppingCart
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void updateCardNum(ShoppingCart shoppingCart) {
        updateById(shoppingCart);
    }

    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public List<ShoppingCart> listbycids(Integer[] cids) {
        List<ShoppingCart> shoppingCartList = list(new QueryWrapper<ShoppingCart>().in("cart_id", cids));
        populate(shoppingCartList);
        return shoppingCartList;

    }

    public void populate(List<ShoppingCart> cartList) {
        List<String> productIdList = cartList.stream().map(shoppingCart -> shoppingCart.getProductId()).collect(Collectors.toList());
        List<Product> productList = productService.list(new QueryWrapper<Product>().in("product_id", productIdList));
        List<ProductImg> productImgList = productImgService.list(new QueryWrapper<ProductImg>().in("item_id", productIdList));
        List<ProductSku> productSkuList = productSkuService.list(new QueryWrapper<ProductSku>().in("product_id", productIdList));
        for (ShoppingCart shoppingCart : cartList) {
            for (Product product : productList) {
                shoppingCart.setProductName(product.getProductName());
            }
            for (ProductImg productImg : productImgList) {
                shoppingCart.setProductImg(productImg.getUrl());
            }
            for (ProductSku productSku : productSkuList) {
                shoppingCart.setSkuName(productSku.getSkuName());
                shoppingCart.setOriginalPrice(productSku.getOriginalPrice());
                shoppingCart.setSellPrice(productSku.getSellPrice());
                shoppingCart.setStock(productSku.getStock());
            }
        }
    }
}







